package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.entity.EquBindEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.service.EquBindService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 设备绑定
 * 
 * @author angjoyo
 *
 */
public class EquBind extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1275569774623574615L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		int r = 0;
		String e = "";

		JSONObject postData = StrUtil.getPostJSON(req);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		JSONObject json = new JSONObject();
		switch (act) {
		case "import":
			int type = data.getInt("type");
			String phone = data.getString("phone");
			String sn = data.getString("sn");
			String brand=data.getString("brand");
			int status = 1;
			UsersEntity users=UsersService.getInstance().findUserByPhone(phone);
			Timestamp addTime = new Timestamp(System.currentTimeMillis());
			EquBindEntity entity = new EquBindEntity();
			entity.setAdd_time(addTime);
			entity.setStatus(status);
			entity.setSn(sn);
			entity.setUser_id(users.getId());
			entity.setBrand(brand);
			entity.setType(type);
			if (EquBindService.getInstance().list(sn, type).isEmpty()) {
				boolean b = EquBindService.getInstance().insert(entity);
				if (b) {
					r = 1;
				} else {
					e = "服务器故障";
				}
			} else {
				e = "该设备已经绑定";
			}
			break;
		case "bind":
			type = data.getInt("type");
			int user_id = data.getInt("user_id");
			sn = data.getString("sn");
			brand=data.getString("brand");
			status = 1;
			addTime = new Timestamp(System.currentTimeMillis());
			entity = new EquBindEntity();
			entity.setAdd_time(addTime);
			entity.setStatus(status);
			entity.setSn(sn);
			entity.setUser_id(user_id);
			entity.setBrand(brand);
			entity.setType(type);
			if(data.containsKey("name")) {
				entity.setName(data.getString("name"));
			}
			if (EquBindService.getInstance().list(sn, type).isEmpty()) {
				boolean b = EquBindService.getInstance().insert(entity);
				if (b) {
					r = 1;
				} else {
					e = "服务器故障";
				}
			} else {
				e = "该设备已经绑定";
			}
			break;
		case "unbind":
			type = data.getInt("type");
			user_id = data.getInt("user_id");
			sn = data.getString("sn");
			entity=EquBindService.getInstance().find(user_id,sn, type);
			if(entity==null) {
				e = "该设备不存在";
			}else {
				entity.setStatus(0);
				boolean b = EquBindService.getInstance().update(entity);
				if (b) {
					r = 1;
				} else {
					e = "服务器故障";
				}
			}
			break;
		case "list_equ":
			user_id = data.getInt("user_id");
			List<EquBindEntity> list=EquBindService.getInstance().listByUserId(user_id);
			JSONArray array=new JSONArray();
			for(EquBindEntity bindEntity:list) {
				JSONObject obj=new JSONObject();
				obj.put("id",bindEntity.getId());
				obj.put("sn", bindEntity.getSn());
				obj.put("time", StrUtil.getYYYYMMDDHHMISS(bindEntity.getAdd_time()));
				obj.put("user_id", bindEntity.getUser_id());
				obj.put("type", bindEntity.getType());
				obj.put("brand", bindEntity.getBrand());
				obj.put("name", bindEntity.getName());
				array.add(obj);
			}
			r=1;
			json.put("d", array);
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
